package com.base.cn.platform.os.controller.pv;

import com.base.cn.platform.os.common.StaticBaseController;
import com.base.cn.platform.os.common.logger.*;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.web.DeviceUtils;
import com.base.cn.platform.os.common.utils.web.WebMobileLoginUtil;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 页面访问日志Controller
 *
 * @author s.li
 * @date 2019/04/16
 */
@RestController
public class PvController extends StaticBaseController {
    @Autowired
    private WebMobileLoginUtil webMobileLoginUtil;

    /**
     * 生成Web端的PV日志
     * @param request
     */
    @RequestMapping("/pv/genPvLogs")
    public void genWebPvLogs(HttpServletRequest request,
                             @RequestParam(value = "requestForm",required = false) String requestForm,
                             @RequestParam(value = "userId",required = false,defaultValue = "0") BigDecimal userId){
        //生成PV日志
        if(StringUtils.isNotEmpty(requestForm)
                && (requestForm.toLowerCase().equals(PageViewLogForm.IOS.getKey().toLowerCase())
                || requestForm.toLowerCase().equals(PageViewLogForm.ANDROID.getKey().toLowerCase())
                || requestForm.toLowerCase().equals(PageViewLogForm.APPLET.getKey().toLowerCase()))){
            PageViewLogger.genPvUvLog(request, requestForm.toUpperCase(), BigDecimal.ZERO);
        }else{
            boolean isMobile = DeviceUtils.isMobileDevice(request);
            if(isMobile){
                PageViewLogger.genPvUvLog(request, PageViewLogForm.MOBILE.getKey(), BigDecimal.ZERO);
            }else{
                PageViewLogger.genPvUvLog(request, PageViewLogForm.PC.getKey(), BigDecimal.ZERO);
            }
        }
        //生成UV日志
        UniqueVisitorLogger.genUvLog(request, BigDecimal.ZERO);
        if(!DataUtil.idIsNotNull(userId)){//重新获取一下缓存中的用户ID
            userId = webMobileLoginUtil.getLoginUserId(request);
        }
        //生成用户活跃日志
        if(DataUtil.idIsNotNull(userId)){
            UserAccessLogger.genDAULog(request,userId,BigDecimal.ZERO);
            //生成终端日志
            this.genTerminalLog(request,userId);
        }
    }

    /**
     * 生成终端日志
     * @param request HttpServletRequest
     * @param userId 用户ID
     */
    private void genTerminalLog(HttpServletRequest request,BigDecimal userId){
        String userAgentStr = request.getHeader("User-Agent");
        UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
        OperatingSystem operatingSystem = userAgent.getOperatingSystem();
        String name = operatingSystem.getName();
        String type ="";
        if(name.startsWith("iOS") || name.contains("iPhone")){//IOS手机
            type = "IOS";
        }else if(name.startsWith("Windows")){//微软电脑
            type ="Windows";
        }else if(name.startsWith("Android")){//安卓手机
            type ="Android";
        }else if(name.startsWith("Mac OS X")){
            if(name.contains("iPhone")){//IOS手机
                type = "IOS";
            }else{//IOS电脑
                type ="Mac";
            }
        }else{
            type = "Other";
        }
        TerminalUnitLogger.genTerminalLog(request,userId,type,BigDecimal.ZERO);
    }
}
